<template>
	<view class="container" :style="{height: style.containerHeight + 'px'}">
<!-- 		<uni-card is-full :border="false">
			<text class="uni-h6" style="text-align: center;">您的微信号暂未绑定餐卡</text>
		</uni-card> -->
		<uni-card is-full :is-shadow="false" :border="false" style="padding-top: 1rem;">
			<uni-forms :modelValue="formData" ref="form" :rules="rules"  :style="{marginTop: style.cardTop + 'px'}">
				<uni-forms-item label="手机号" name="mobile" labelWidth="110px" required>
					<uni-easyinput type="text" v-model="formData.mobile"/>
				</uni-forms-item>
				<uni-forms-item label="身份证号" name="idno" labelWidth="110px" required>
					<uni-easyinput type="text" v-model="formData.idno" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" class="uni-primary" style="background-color: #4c94d9;"  @click="submitForm">绑定</button>


		</uni-card>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="popup.msgType" :message="popup.msg" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>

</template>

<script>
	//绑定已有用户信息
	export default {
		data() {
			return {
				style: {
					cardTop: 20,
					containerHeight: 500
				},
				popup: {
					msg: "-",
					msgType: "success",
				},
				formData: {
					mobile: "",
					idno: "",
					openid: "",
				},
				rules: {
					mobile: {
						rules: [
							{
								required: true,
								errorMessage: "请输入手机号"
							},
						]
					},
					idno: {
						rules: [{
							required: true,
							errorMessage: "请输入身份证号"
						}]
					}
				}
			}
		},
		onReady() {
			let that = this;
			this.$refs.form.setRules(this.rules);
			const res = uni.getSystemInfo({
				success: (res) => {
					that.style.containerHeight = res.windowHeight;
					that.style.cardTop = (res.windowHeight - 243) / 3
				}
			});
			uni.getStorage({
				key: 'wx_openid',
				success: function(res) {
					that.formData.openid = res.data
				}
			});
		},
		methods: {
			submitForm() {
				let that = this;
				this.$refs.form.validate().then(res => {
					uni.request({
						url: that.baseUrl + '/wx/user/bindUserInfo2',
						method: 'POST',
						data: that.formData,
						success(res) {
							if (res.data.type === 'success') {
								that.popup.msg = "绑定成功";
								that.popup.msgType = 'success';
								that.$refs.message.open()
								setTimeout(function(){
									uni.switchTab({
										url: "/pages/index/index"
									})
								},1000)
							
							} else {
								that.popup.msg = res.data.msg;
								that.popup.msgType = 'error';
								that.$refs.message.open()
							}
						},
						fail(res) {

						}
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			scanCard() {
				let that = this;
				uni.scanCode({
					onlyFromCamera: true,
					success: function (res) {
						that.formData.cardNo = res.result;
					}
				});
			},
			dialogClose() {

			},
		
		},

	}
</script>


<style>
</style>